package com.c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c.entity.CheckItem;
import com.c.entity.Permission;
import com.c.entity.Role;
import com.c.entity.User;
import com.c.mapper.CheckltemMapper;
import com.c.mapper.PermissionMapper;
import com.c.mapper.RoleMapper;
import com.c.mapper.UserMapper;
import com.c.service.CheckltemService;
import com.c.service.UserService;
import com.c.utils.MessageConstant;
import com.c.utils.PageResult;
import com.c.utils.QueryPageBean;
import com.c.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author wangxiangli
 * @create 2022-12-25-12:30
 */
@Service
@Transactional
public class CheckltemServiceImpl extends ServiceImpl<CheckltemMapper, CheckItem> implements CheckltemService {


    /**
     * 检查项分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        IPage<CheckItem> pageCheckItem=new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<CheckItem> wrapper=new QueryWrapper();
        String queryString = queryPageBean.getQueryString();
        //判断条件值是否为空，如果不为空拼接条件
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            queryString = queryPageBean.getQueryString().trim();// 去除查询条件首尾空格
            wrapper.like("name",queryString);
            wrapper.or().like("code",queryString);
            wrapper.orderByDesc("id");
        }

        IPage page1 = baseMapper.selectPage(pageCheckItem, wrapper);
        return new PageResult(page1.getTotal(),page1.getRecords());

    }
    /**
     * 检查项分页查询(优化分页查询)
     *1.统计所有的数据（必须要返回的数据）
     * 2.比较要分页查询的数据是开始位置是否大于总数据，大于则不查，小于则继续查
     * 3.根据查询条件查询出范围值的所有di数组
     * 4.拿着对应的id数组走主键索引去查询对应的数据
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery2(QueryPageBean queryPageBean) {
        //统计所有的数据
        Long total = baseMapper.selectCount(null);
        if(total>=queryPageBean.getPageSize()*(queryPageBean.getCurrentPage()-1)){
            Integer[] checkItemids=baseMapper.selectIdsByCondtion(queryPageBean.getQueryString(),queryPageBean.getPageSize()*(queryPageBean.getCurrentPage()-1),queryPageBean.getPageSize());
            if(checkItemids==null){
                return null;
            }
            List<CheckItem> checkItems=baseMapper.selectCheckItems(checkItemids);
            return new PageResult(total,checkItems);
        }
    return null;
    }




    /**
     * 根据ID删除检查项
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        //判断当前检查项是否已经关联到检查组
        long count = baseMapper.findCountByCheckItemId(id);
        if (count > 0) {
            //当前检查项已经被关联到检查组，不允许删除
            return new Result(false, "检查项被使用,无法删除");
        } else {
            baseMapper.deleteById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }
    }

}
